#!/bin/sh
################################################################################
# ssh_server
#
# This script configures the ssh server.
################################################################################
. /etc/rc.d/functions

start() {
    /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/ssh_server] Starting..."

    if [ -e /etc/ssh/sshd_config ] ; then
        /bin/chmod 0600 /etc/ssh/sshd_config
        /bin/chown root:root /etc/ssh/sshd_config
    fi

    if [ -e /etc/ssh/ssh_host_rsa_key ] ; then
        /bin/chmod 0600 /etc/ssh/ssh_host_rsa_key
        /bin/chown root:root /etc/ssh/ssh_host_rsa_key
    fi

    if [ -e /etc/ssh/authorized_keys ] ; then
        /bin/chmod 0600 /etc/ssh/authorized_keys
        /bin/chown root:root /etc/ssh/authorized_keys

        /bin/mkdir -p /root/.ssh
        /bin/cp /etc/ssh/authorized_keys /root/.ssh/authorized_keys
        /bin/chmod 0600 /root/.ssh/authorized_keys
        /bin/chown root:root /root/.ssh/authorized_keys
    fi

    if [ "x${MM_SSH_SERVER_ENABLED}" = "xyes" ] ; then
        if [ ! -n "`/bin/pidof sshd`" ] ; then
            /bin/mkdir -p /var/empty
            /usr/sbin/sshd
        fi
    fi

    /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/ssh_server] Started with return code=0..."

    return 0
}

stop() {
    if [ -n "`/bin/pidof sshd`" ] ; then
        mm_message_output info "stopping ssh server ..."
        /usr/bin/killall sshd
    fi

    return 0
}

case $1 in
    start) start ;;
    stop)  stop  ;;
esac

exit 0
